#region Import Declarations

using System;
using System.Text;
using System.Xml;
using System.IO;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;

#endregion

namespace CommunityServerStuff.Writers
{
    /// <summary>
    /// The base class for XmlWriters in CommunityServerStuff
    /// </summary>
    class WriterBase : XmlTextWriter
    {

        #region Private Members

        private Section _currentSection;
        private CSContext _csContext;
        private SiteSettings _siteSettings;
        private StringWriter _sw;
        private bool isBuilt;

        #endregion

        #region Properties

        #region CSContext

        /// <summary>
        /// 
        /// </summary>
        public CSContext CSContext
        {
            get
            {
                return _csContext;
            }
        }

        #endregion

        #region SiteSettings

        /// <summary>
        /// 
        /// </summary>
        public SiteSettings SiteSettings
        {
            get
            {
                return _siteSettings;
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Default Constructor

        /// <summary>
        /// 
        /// </summary>
        /// <param name="siteSettings"></param>
        /// <param name="sw"></param>
        public WriterBase(SiteSettings siteSettings, StringWriter sw) : base(sw)
        {
            _csContext = CSContext.Current;
            _siteSettings = _csContext.SiteSettings;
            _sw = sw;

        }

        #endregion

        #region Build

        /// <summary>
        /// 
        /// </summary>
        public virtual void Build()
        {
        }

        #endregion

        #region GetCurrentWeblog

        /// <summary>
        /// 
        /// </summary>
        protected Section GetCurrentWeblog
        {
            get
            {
                if (_currentSection == null)
                {
                    _currentSection = Weblogs.GetWeblog(_csContext.ApplicationKey);
                    Permissions.AccessCheck(_currentSection, Permission.View, _csContext.User);
                }
                return _currentSection;
            }
        }

        #endregion

        #region GetXml

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetXml()
        {
            if (!(isBuilt))
            {
                Build();
                isBuilt = true;
            }
            return _sw.ToString();
        }

        #endregion

        #region ToString

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return GetXml();
        }

        #endregion

        #endregion

    }
}
